//package com.monitor.app_monitor.service.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.hl.opnc.AbilityResponse;
//import com.hl.opnc.ResponseBody;
//import com.hl.opnc.invoke.AbilityInvokeManager;
//import com.hl.opnc.invoke.InvokeManager;
//import com.monitoring.exportercollect.Config.EmailBO;
//import com.monitoring.exportercollect.model.DTO.*;
//import com.monitoring.exportercollect.service.ReportMsg;
//import com.monitoring.exportercollect.service.quartz.utils.CreateSign;
//import com.monitoring.exportercollect.service.quartz.utils.HttpClientUtil;
//import freemarker.template.Configuration;
//import freemarker.template.Template;
//import freemarker.template.TemplateException;
//import io.micrometer.core.instrument.util.StringUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//@Service
//@Slf4j
//public class DingDingMsgLmpl implements ReportMsg {
//    /**
//     * 发送钉钉消息
//     *
//     * @param webhook 钉钉官方指定DTO
//     * @return
//     */
//    @Value("${email.url}")
//    private String url;
//    private static final InvokeManager manager =new AbilityInvokeManager("fc4a9dda02eb47b58ea77018412cb331","a2becb5321e44a5b9350fe3ad9861a73");
//    @Resource
//    private Configuration configuration;
//    @Override
//    public int sendDingDingMsg(Webhook webhook) {
//        try {
//            final String[] sign = CreateSign.getSign();
//            String url = "https://oapi.dingtalk.com/robot/send?access_token=48af719ee039557618f212a345c7b069fc2304659d9b6b6cbb2bfd97888e2f4c&timestamp="+sign[0]+"&sign="+sign[1];
//
//
//            webhook.getAlerts().forEach( al -> {
//                final Text text = new Text();
//
//                text.setContent(al.getAnnotations().getDescription());
//
//
//                DingdingDTO dingdingDTO = new DingdingDTO();
//                dingdingDTO.setMsgtype("text");
//                dingdingDTO.setText(text);
//                final String js = JSONObject.toJSONString(dingdingDTO);
//                Map<String,String> headMap = new HashMap<>();
//                headMap.put("Content-Type","application/json; charset=utf-8");
//                headMap.put("timestamp",sign[0]);
//                headMap.put("sign",sign[1]);
//                final String res = HttpClientUtil.doPostJson(url, js, headMap);
//                log.info(res);
//            });
//
//
//           // String js = "{\"at\":{\"atMobiles\":[\"180xxxxxx\"],\"atUserIds\":[\"user123\"],\"isAtAll\":false},\"text\":{\"content\":\"我就是我, @XXX 是不一样的烟火\"},\"msgtype\":\"text\"}";
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return  1;
//    }
//
//    @Override
//    public void sendMileMsg(Webhook webhook) {
//        webhook.getAlerts().forEach( al -> {
//            try{
//                //private String status;
//                //private Labels labels;
//                //private Annotations annotations;
//                //private String generatorURL;
//
//                Map<String,Object> model = new HashMap<>();
//                model.put("status", al.getStatus());
//                model.put("labels", al.getLabels());
//                model.put("annotations", al.getAnnotations());
//                model.put("generatorURL", al.getGeneratorURL());
//                Template template = configuration.getTemplate("mail.ftl");
//                String text = "";
//                try{
//                    text = FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
//                }catch (TemplateException e){
//                    log.error("邮件模板处理失败", e);
//                }
//
//                if (StringUtils.isNotBlank(text)) {
//                    EmailBO emailBO = new EmailBO();
//                    emailBO.setMsgSubject("测试");
//                    emailBO.setMsgText(text);
//                    List<String> mailAdss = new ArrayList<>();
//                    mailAdss.add("776154852@qq.com");
//                    emailBO.setMsgAddress(mailAdss);
//                    log.info("开始发送邮件 emailBO={}",emailBO);
//
//                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(emailBO);
//                    String emailMsg = jsonObject.toString();
//                    log.info("emailMsg {}",emailMsg);
//                    AbilityResponse response = manager.post(url,null,emailMsg);
//                    try {
//                        ResponseBody responseBody = response.getRespObj();
//                        log.info("邮件发送完成  响应头={} 返回内容={}",responseBody.getUNI_NET_BODY(),responseBody.getUNI_NET_HEAD());
//                    } catch (IOException e) {
//                        log.error("邮件发送失败",e);
//                    }finally {
//                        try {
//                            response.close();
//                        } catch (IOException e) {
//                            log.error("中台连接关闭失败",e);
//                        }
//                    }
//                }
//            }catch (Exception e){
//                log.error("处理发送邮件逻辑失败", e);
//            }
//        });
//    }
//}
////